//
//  LESelectGoodsCategoryController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/15.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LESelectGoodsCategoryController.h"
#import "JSDropDownMenu.h"

@interface LESelectGoodsCategoryController ()<JSDropDownMenuDataSource,JSDropDownMenuDelegate>
{
    NSMutableArray *_data1;
    NSMutableArray *_data2;
    NSMutableArray *_data3;
    
    NSInteger _currentData1Index;
    NSInteger _currentData2Index;
    NSInteger _currentData3Index;

    JSDropDownMenu *menu;
}
/**
 种类
 */
@property(nonatomic,copy)NSString *gc_id;
/**
 哪一级
 */
@property(nonatomic,copy)NSString *deep;


@end

@implementation LESelectGoodsCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择商品的种类";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    // 指定默认选中
    _currentData1Index = 0;
    
    _data1 = [NSMutableArray array];
    _data2 = [NSMutableArray array];
    _data3 = [NSMutableArray array];
    
    menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:45];
    menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    menu.dataSource = self;
    menu.delegate = self;
    
    [self.view addSubview:menu];
}
- (void)loadData
{
    self.gc_id = @"0";
    self.deep = @"1";
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.gc_id forKey:@"gc_id"];
    [dict setObject:self.deep forKey:@"deep"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreSelectGoodsCategoryUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSArray class]])
        {
           
            if ([weakSelf.deep isEqualToString:@"1"]) {
                [_data1 removeAllObjects];
                [_data1 addObjectsFromArray:datas];
            }
            if ([weakSelf.deep isEqualToString:@"2"]) {
                [_data2 removeAllObjects];
                [_data2 addObjectsFromArray:datas];
            }
            if ([weakSelf.deep isEqualToString:@"3"]) {
                [_data3 removeAllObjects];
                [_data3 addObjectsFromArray:datas];
            }


        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- JSDropDownMenuDataSource
/**
 返回三列
 
 @param menu <#menu description#>
 @return <#return value description#>
 */
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}

/**
 是否有右边表格
 
 @param column <#column description#>
 @return <#return value description#>
 */
-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    return NO;
}


/**
 左边一列宽度比例
 
 @param column <#column description#>
 @return <#return value description#>
 */
-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    return 1;
}


/**
 当前左边选中的行数赋值:
 
 @param column <#column description#>
 @return <#return value description#>
 */
-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        if (_data2.count == 0) {
            [SVProgressHUD showInfoWithStatus:@"您还没有选中一级分类"];
        }
        return _currentData2Index;
    }
    if (column==2) {
        if (_data3.count == 0) {
            [SVProgressHUD showInfoWithStatus:@"您还没有选中二级分类"];
        }
        return _currentData3Index;
    }
    return 0;
}


/**
 返回一列有多少行
 
 @param menu <#menu description#>
 @param column <#column description#>
 @param leftOrRight <#leftOrRight description#>
 @param leftRow <#leftRow description#>
 @return <#return value description#>
 */
- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    if (column==0) {
        return _data1.count;
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}


/**
 每一列的标题
 
 @param menu <#menu description#>
 @param column <#column description#>
 @return <#return value description#>
 */
- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {

        case 0: return @"请选择一级分类";
            break;
        case 1: return @"请选择二级分类";
            break;
        case 2: return @"请选择三级分类";
            break;
        default:
            return nil;
            break;
    }
}

/**
 每一行的标题
 
 @param menu <#menu description#>
 @param indexPath <#indexPath description#>
 @return <#return value description#>
 */
- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        return _data1[indexPath.row][@"gc_name"];
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row][@"gc_name"];
        
    } else {
        
        return _data3[indexPath.row][@"gc_name"];
    }
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0)
    {
        _currentData1Index = indexPath.row;
        NSDictionary *dict = _data1[indexPath.row];
        self.gc_id = dict[@"gc_id"];
        self.deep = @"2";
        [self requestRefreshData];
        
    } else if(indexPath.column == 1)
    {
        _currentData2Index = indexPath.row;
        NSDictionary *dict = _data2[indexPath.row];
        self.gc_id = dict[@"gc_id"];
        self.deep = @"3";
        [self requestRefreshData];
        
    } else{
        
        _currentData3Index = indexPath.row;
    }
}
#pragma mark- 点击事件
- (void)sureClick
{
    NSString *str1 = _data1[_currentData1Index][@"gc_name"];
    NSString *str2 = _data2[_currentData2Index][@"gc_name"];
    NSString *str3 = _data3[_currentData3Index][@"gc_name"];
    NSString *cateId = _data3[_currentData3Index][@"gc_id"];

    NSString *selectedStr01 = [NSString stringWithFormat:@"%@>%@>%@",str1,str2,str3];
    NSString *selectedStr = [self removeSpaceAndNewline:selectedStr01];
    LELog(@"%s---%d---%@",__func__,__LINE__,selectedStr);
    [self.navigationController popViewControllerAnimated:YES];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:selectedStr forKey:@"cate_name"];
    [dict setObject:cateId forKey:@"cate_id"];
    [[NSNotificationCenter defaultCenter] postNotificationName:Select_GoodsCategory_Notification object:dict];
    
}
- (NSString *)removeSpaceAndNewline:(NSString *)str
{
    NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return temp;
}

@end
